﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Manageweb_Member_ScoreTree : System.Web.UI.Page
{

    DataView dv;
    DataTable dt;
    public string StrKey;

    protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);

        if (!hidMiYao.Value.Equals("1"))
        {
            writer.Write(sw.ToString().Replace("<body", "<body onload=javascript:openLayer('test','test_con');"));
        }
        else
        {
            writer.Write(sw);
        }
    }


    //获取密钥Cdefine5
    public string CheckData()
    {
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30)
            };
        paras[0].Value = Session["userid"];

        StrKey = SQLExpressDAL.SqlHelper.FillDataSet(CommandType.StoredProcedure, "UP_GetUserKey", paras).Tables[0].Rows[0][1].ToString();
        return StrKey;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        StrKey = CheckData();
        if (hidMiYao.Value.Equals("1"))
        {
            this.DivScoreTree.Visible = true;

            SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
            SQLExpressDAL.Users.UsersDataTable userTable = userAdp.GetAllUsers();
            dt = (DataTable)userTable;


            SQLExpressDAL.Users.UsersDataTable singleUserTable = userAdp.GetSingleUserInfo(Session["UserID"].ToString());
            DataRow row = singleUserTable.Rows[0];
            this.TreeView1.Nodes.Clear();
            //创建根节点 
            TreeNode root = new TreeNode();
            //必须与数据库的对应 
            root.Text = string.Format(" |[{0}] | {1} | {2} | {3}", row["userid"].ToString(), row["username"].ToString(), row["LjScore"].ToString(), row["SpecialScore"].ToString());

            root.Value = row["userid"].ToString();
            //添加根节点 
            this.TreeView1.Nodes.Add(root);

            //第一次加载时调用方法传参 
            CreateTree(Session["UserID"].ToString(), root, dt, this.TreeView1);

           
            this.TreeView1.ExpandAll();
        }


    }

    /// <summary> 
    /// 创建一个树 
    /// </summary> 
    /// <param name="parentID">父ID</param> 
    /// <param name="node">节点</param> 
    /// <param name="dt">DataTable</param> 
    /// <param name="treeView">TreeView的名称</param> 
    public void CreateTree(String parentID, TreeNode node, DataTable dt, TreeView treeView)
    {
        //实例化一个DataView dt = 传入的DataTable 
        dv = new DataView(dt);
        //筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID) 
        dv.RowFilter = " [cdefine1]='" + parentID + "'";
        string template = "[A区]| [{0}] | {1} | {2} |{3}";
        string ljScore = "";
        string specialScore = "";

        //bool flag = ComputeSpecial(parentID, dt);
        //double sum = 0;
        //if (flag)
        //{
           
        //    foreach (DataRowView row in dv)
        //    {
        //        string sScore = row["SpecialScore"].ToString();
        //        sum += string.IsNullOrEmpty(sScore) ? 0 : Convert.ToDouble(sScore);
        //    }

        //}

        //用foreach遍历dv 
        foreach (DataRowView row in dv)
        {
            TreeNode childNode = new TreeNode();
            //第一次加载时为空 
            //if (node == null)
            //{
            //    //创建根节点 
            //    //TreeNode root = new TreeNode();
            //    //必须与数据库的对应 
            //    ljScore = row["LjScore"].ToString();
            //    ljScore = string.IsNullOrEmpty(ljScore) ? "0.00" : ljScore;

            //    specialScore = row["SpecialScore"].ToString();
            //    specialScore = string.IsNullOrEmpty(specialScore) ? "0.00" : specialScore;

            //    childNode.Text = string.Format(template, row["userid"].ToString(), row["username"].ToString(), ljScore, specialScore);
            //    //root.SelectAction = TreeNodeSelectAction.None;
            //    childNode.Value = row["userid"].ToString();
            //    //添加根节点 
            //    this.TreeView1.Nodes.Add(childNode);
            //    //递归调用方法本身 
            //    CreateTree(row["userid"].ToString(), childNode, dt, treeView);
            //    template = "[B区] | [{0}] | {1} |{2} | {3}";
            //}
            //else
            //{
            //node.Text += "|"+sum;
                ljScore = row["LjScore"].ToString();
                ljScore = string.IsNullOrEmpty(ljScore) ? "0.00" : ljScore;

                specialScore = row["SpecialScore"].ToString();
                specialScore = string.IsNullOrEmpty(specialScore) ? "0.00" : specialScore;

                //添加子节点 
                //TreeNode childNode = new TreeNode();
                childNode.Text = string.Format(template, row["userid"].ToString(), row["username"].ToString(), ljScore, specialScore);
                childNode.Value = row["userid"].ToString();
                //childNode.SelectAction = TreeNodeSelectAction.None;
                node.ChildNodes.Add(childNode);
                CreateTree(row["userid"].ToString(), childNode, dt, treeView);
                template = "[B区] | [{0}] | {1} |{2} | {3}";
 
            //}

            
        }
    }


    public bool ComputeSpecial(String parentID, DataTable dt)
    {
        bool flag = false;

        //实例化一个DataView dt = 传入的DataTable 
        dv = new DataView(dt);
        //筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID) 
        dv.RowFilter = " [cdefine1]='" + parentID + "'";

        foreach (DataRowView row in dv)
        {
            string zhituiren = row["parentID"].ToString();

            if (zhituiren.Equals(parentID))
            {
                flag = true;
                break;
            }
            ComputeSpecial(row["userid"].ToString(), dt);

        }

        return flag;

    }

}
